namespace Xmef.Engine
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Xmef.Interfaces;
    using Xmef.Library;

    [Serializable]
    public class PropertyCollection<T> : PluginIdentity, IPropertyCollection<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IPluginIdentity where T: IProperty
    {
        private readonly Dictionary<string, T> _dictionary;

        public PropertyCollection()
        {
            this._dictionary = new Dictionary<string, T>();
        }

        public PropertyCollection(PropertyCollection<T> dictionary)
        {
            this._dictionary = new Dictionary<string, T>(dictionary._dictionary);
        }

        public void Add(T property)
        {
            if (!this._dictionary.ContainsKey(property.Name))
            {
                this._dictionary.Add(property.Name, property);
            }
        }

        public void Clear()
        {
            this._dictionary.Clear();
        }

        public bool Contains(T item)
        {
            return this._dictionary.ContainsKey(item.Name);
        }

        public void CopyTo(T[] array, int index)
        {
            this._dictionary.Values.CopyTo(array, index);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return (IEnumerator<T>) this._dictionary.Values.GetEnumerator();
        }

        public bool Remove(T property)
        {
            return (this._dictionary.ContainsKey(property.Name) && this._dictionary.Remove(property.Name));
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public int Count
        {
            get
            {
                return this._dictionary.Count;
            }
        }

        public T this[string name]
        {
            get
            {
                if (this._dictionary.ContainsKey(name))
                {
                    return this._dictionary[name];
                }
                return default(T);
            }
        }

        bool ICollection<T>.IsReadOnly
        {
            get
            {
                return false;
            }
        }
    }
}

